﻿
@{
    ViewData["Title"] = "部门信息";
    Layout = "~/Views/Shared/_Index.cshtml";
}
    <link href="~/ui/plugins/jquery-ztree/3.5/css/metro/zTreeStyle.css" rel="stylesheet" />
    <div class="container-div white-bg">
        <div class="ibox-content">
            <form class="form-horizontal m" id="deptForm" autocomplete="off">
                <input type="hidden" id="id" name="id" value="@(ViewBag.Id)">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">上级：</label>
                    <div class="col-sm-8">
                        <input class="form-control" type="hidden" name="parent_id" id="parent_id">
                        <input type="text" class="form-control" id="parentDept" checks="0" readonly textLabel="jasontext" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">单位名称：</label>
                    <div class="col-sm-8">
                        <input class="form-control" type="text" name="dept_name" id="dept_name" data-rule="required">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">负责人：</label>
                    <div class="col-sm-8">
                        <div class="input-group">
                            <input class="form-control" type="hidden" name="leader_id" id="leader_id" readonly>
                            <input class="form-control" type="text" name="leader" id="leader" readonly>
                            <span class="input-group-btn">
                                <button type="button" class="btn btn-danger" style="height: 31px;" onclick="clearUser();">清除</button>
                                <button type="button" class="btn btn-primary" style="height: 31px;" onclick="selectUser();">选择</button>
                            </span>
                        </div>

                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">显示顺序：</label>
                    <div class="col-sm-8">
                        <input class="form-control" type="number" name="order_num" value="1" id="order_num" data-rule="required">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">联系电话：</label>
                    <div class="col-sm-8">
                        <input class="form-control" type="text" name="phone" id="phone">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">邮箱：</label>
                    <div class="col-sm-8">
                        <input class="form-control" type="text" name="email" id="email" data-rule="email;">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">部门类型：</label>
                    <div class="col-sm-8">

                        <label class="check-box checkbox-success">
                            <input type="radio" class="icheck" id="dept_type" name="dept_type" value="0" />
                            分公司
                        </label>
                        <label class="check-box checkbox-success">
                            <input type="radio" class="icheck" id="dept_type1" name="dept_type" value="1" checked="" />
                            部门
                        </label>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">状态：</label>
                    <div class="col-sm-8">

                        <label class="check-box checkbox-success">
                            <input type="radio" class="icheck" id="dept_status" name="dept_status" value="0" checked="" />
                            启动
                        </label>
                        <label class="check-box checkbox-success">
                            <input type="radio" class="icheck" id="dept_status1" name="dept_status" value="1" />
                            停用
                        </label>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">备注：</label>
                    <div class="col-sm-8">
                        <input id="remark" name="remark" class="form-control" type="text">
                    </div>
                </div>
                <div class="footerbar">
                    <div class="col-sm-12 col-sm-offset-3">
                        <button class="btn btn-primary" type="submit">
                            <i class="fa fa-check"></i>
                            保存
                        </button>
                        <button class="btn btn-danger" type="button" onclick="cancel()">
                            <i class="fa fa-reply-all"></i>关闭
                        </button>
                    </div>
                </div>
            </form>

        </div>
    </div>
@section scripts{
    <script src="~/ui/plugins/jquery-ztree/3.5/js/jquery.ztree.all-3.5.js"></script>
    <script src="~/ui/easycms/js/multiple-tree-select.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#deptForm').validator({
                stopOnError: false,
                timely: 2,
                theme: "yellow_right",
                valid: function (form) {
                    //console.log(form);
                    let parentIds = $('#parentDept_checkedIds').val();
                    $('#parent_id').val(parentIds);
                    jutils.ajaxPost("/admin/SysDepartment/save", $(form).serialize(), function (res) {
                        if (res.status) {
                            cancel();
                        }
                    });
                },
            });
            jutils.handleMessage(function (res) {
                console.log(res);
                if (res.title == 'userSelect') {
                    $('#leader').val(res.data.user_name);
                    $('#leader_id').val(res.data.id);
                }

            });
            loadModel();
        });
        function loadModel() {
            let id = $('#id').val();

            jutils.ajaxGet('/admin/SysDepartment/getmodel', { id: id }, function (res) {
                if (res.status) {
                    if (!jutils.emptyId(id)) {
                        let model = res.data.model;
                        $('#deptForm').initFormData(model);
                        $('#parentDept').attr('checks', model.parent_id);
                    }
                    initSelectTree(res.data.ztreeData);
                }
            });
        }


        function selectUser() {
            let url = '/admin/sysuser/userSelect';
            jutils.dialog('选择负责人', url);
        }

        function clearUser() {
            $('#leader').val('');
            $('#leader_id').val(0);
        }

        function initSelectTree(treeNodes) {
            let defaults = {
                callback: {
                    onCheck: zTreeOnCheck
                },
                chkStyle: "radio",
                zNodes: treeNodes,
                height: 333   // 默认自定义下拉框高度
            }

            let tree = $("#parentDept").drawMultipleTree(defaults);

        }
        function zTreeOnCheck(treeNodes) {

        };
        function cancel() {
            jutils.closeDialog();
        }

    </script>
}

